import java.awt.AWTException;
import java.awt.Image;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.UIManager;

/*
 *
 *
 */
public class Tray {

	private ImageIcon icon = null;	//Icon for menu items
	private TrayIcon trayicon = null;
	private SystemTray tray = null;
	private JPopupMenu popup = null;
	private JMenuItem menuItemEmailInformation = null;
	private JMenuItem menuItemVerifyMessg = null;
	private JMenuItem menuItemTimeout = null;
	private JMenuItem menuItemInsertEmail = null;
	private JMenuItem menuItemRemoveEmail = null;
	private JMenuItem menuItemExit = null;
	
	//private Comunication comunication;
	
	
	
	public Tray(){
		//font = new Font(arg0);
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			//new ModError("Cannot set Ambience");
			System.err.println("Cannot Set Ambience: " + e.toString());
		}
		
		createObjects();	//Create all main Object
		displayObjects();	//Display all objects created
		
	}

	private void createObjects(){
		
		trayicon = new TrayIcon(createImage("images/iconTray.gif", "Email AutoChecker"),"Email AutoChecker", null);
		trayicon.addMouseListener(new TrayListener(this));
		tray = SystemTray.getSystemTray();
		popup = new JPopupMenu();

		menuItemEmailInformation = new JMenuItem("No Account"); 
		menuItemEmailInformation.setEnabled(false);
		
		
		icon = new ImageIcon(getClass().getResource("images/verify.png"));
		menuItemVerifyMessg = new JMenuItem("Verify Unread Messages", icon);
		icon = new ImageIcon(getClass().getResource("images/timeout.png"));
		menuItemTimeout = new JMenuItem("Set Timeout", icon);
		icon = new ImageIcon(getClass().getResource("images/add.png"));
		
		menuItemInsertEmail = new JMenuItem("Insert Email", icon);
		menuItemInsertEmail.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				//new Account(email, password);
				//Quando se cria o account faz-se logo a a comunicacao com o server
			}
			
		});
		
		
		
		icon = new ImageIcon(getClass().getResource("images/close.png"));
		menuItemRemoveEmail = new JMenuItem("Remove Email", icon);
		menuItemExit = new JMenuItem("Exit");
		
		menuItemExit.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}
		});

	}
	
	private void displayObjects(){
		
		popup.add(menuItemEmailInformation);
		popup.addSeparator();
		popup.add(menuItemVerifyMessg);
		popup.add(menuItemTimeout);
		popup.addSeparator();
		popup.add(menuItemInsertEmail);
		popup.add(menuItemRemoveEmail);
		popup.addSeparator();
		popup.add(menuItemExit);
		
		try {
			tray.add(trayicon);
		} catch (AWTException e) {
			//new ModError("Cannot access SystemTray");
			System.err.println("Cannot Access System Tray: " + e.toString());
			System.exit(0);
		}
		
		
		
	}
	
	protected static Image createImage(String path, String description) {
		URL imageURL = Tray.class.getResource(path);

		if (imageURL == null) {
			System.err.println("Resource not found: " + path);
			return null;
		} else {
			return (new ImageIcon(imageURL, description)).getImage();
		}
	}
	
		public SystemTray getTray() {
			return tray;
		}
	
	
		public void setTray(SystemTray tray) {
			this.tray = tray;
		}
	
	
		public JPopupMenu getPopup() {
			return popup;
		}
	
	
		public void setPopup(JPopupMenu popup) {
			this.popup = popup;
		}
	
}